/*
 *  Copyright 2011 yura.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */

package org.opu.vcforms.transform_t2d;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Properties;
import org.opu.vcforms.datasource.DataReceiver;
import org.opu.vcforms.datasource.Storage;
import org.opu.vcforms.transform_t2d.content.ContentAPI;
import org.opu.vcforms.transform_t2d.content.ContentProcessor;

/**
 *
 * @author yura
 */
public class MainProcessor {

    public static final String LINE_SEPARATOR = "line.separator";
    public static final String SYSTEM_PREFIX = "$";
    public static final int PREFIX_LENGTH = SYSTEM_PREFIX.length();

    /** By default configuration does not contain properties, because they are
     * determined at time of transformation */
    protected final SystemConfiguration configuration;
    protected final DataReceiver dbManager;
    protected final Map<String/* name */, Storage/* data */> userSettings;
    protected final Properties systemSettings;
    /** Resolve system calls (system fields). */
    protected final ContentAPI contentProcessor;

    /**
     *  Create new instance
     * @param receiver needed to send query and get request from database
     * @param userSettings user data (can be null or empty).
     * @param systemSettings systemSettings system settings (course, semestr, ...).
     *  If it null loads system properties - {@link System#getProperties()}.
     * @param contentProcessor content (can be null)
     * @param configuration 
     */
    protected MainProcessor(DataReceiver dbManager, Map<String, Storage> userSettings,
            Properties systemSettings, ContentAPI contentProcessor,
            SystemConfiguration configuration) {

        if (userSettings == null) {
            userSettings = new LinkedHashMap<String, Storage>();
        }

        if (systemSettings == null) {
            systemSettings = new Properties(System.getProperties());
        }

        this.configuration = configuration;
        this.dbManager = dbManager;
        this.userSettings = userSettings;
        this.systemSettings = systemSettings;
        this.contentProcessor = contentProcessor;

        Properties prop = configuration.getProperties();
        for (String name : systemSettings.stringPropertyNames()) {
            if (name != null && name.startsWith(SYSTEM_PREFIX)){
                prop.setProperty(name.substring(PREFIX_LENGTH),
                        systemSettings.getProperty(name));
            }
        }

        if (!prop.containsKey(LINE_SEPARATOR)){
            System.err.println("WARNING: line separator is not defined.");
            prop.setProperty(LINE_SEPARATOR, System.getProperty(LINE_SEPARATOR));
        }
    }

    /**
     *  Create new instance
     * @param receiver needed to send query and get request from database
     * @param userSettings can be empty (user data).
     * @param systemSettings systemSettings system settings (course, semestr, ...).
     *  If it null loads system properties - {@link System#getProperties()}.
     * @param contentProcessor content (can be null)
     */
    public MainProcessor(DataReceiver receiver, Map<String, Storage> userSettings,
            Properties systemSettings, ContentProcessor contentProcessor) {

        this(receiver, userSettings, systemSettings, contentProcessor,
                new SystemConfiguration(new Properties()));
    }

    /**
     *  Create new instance with nullable content (for main node - document).
     * @param receiver needed to send query and get request from database
     * @param userSettings can be empty (user data).
     * @param systemSettings systemSettings system settings (course, semestr, ...).
     *  If it null loads system properties - {@link System#getProperties()}.
     */
    public MainProcessor(DataReceiver receiver, Map<String, Storage> userSettings,
            Properties systemSettings) {

        this(receiver, userSettings, systemSettings, null);
    }

    public ContentAPI getContentProcessor() {
        return contentProcessor;
    }

    public DataReceiver getDataReceiver() {
        return dbManager;
    }

    public Properties getSystemSettings() {
        return systemSettings;
    }

    public Map<String, Storage> getUserSettings() {
        return userSettings;
    }

    public SystemConfiguration getSystemConfiguration() {
        return configuration;
    }

    /**
     * Return user defined line separator
     * @return user defined line separator
     */
    public final String getLineSeparator(){
        return configuration.getProperties().getProperty(LINE_SEPARATOR);
    }
}
